﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MyCraft.Minecraft_2D.Blocs;

namespace MyCraft.Minecraft_2D
{
    class SelectBloc
    {
        #region Attributs
        int _count;
        int _id;
        TypeBloc[] _bloc;
        Texture2D _select;
        Vector2 _position;
        #endregion

        #region Constructeur
        public SelectBloc(Vector2 position)
        {
            _count = 9;
            _id = 0;
            _bloc = new TypeBloc[_count];
            _position = position;
        }
        #endregion

        #region Propriétés
        public Blocs.Bloc SelectedBloc
        {
            get { return BlocSet.GetSingleton()[_bloc[_id]]; }
            set { _id = value.Id; }
        }
        #endregion

        #region Methodes

        #endregion

        #region XNA
        public void LoadContent(ContentManager content)
        {
            _select = content.Load<Texture2D>(@"Blocs/Selected");
            int j = 0;
            for (int i = 0; i < _count; i++)
            {
                _bloc[i] = (TypeBloc)j;
                if (!BlocSet.GetSingleton()[_bloc[i]].OnInventory())
                    --i;
                ++j;
            }
        }

        public void Update(int scrool)
        {
            _id += scrool % _count;
            if (_id > _count - 1)
                _id = 0;
            else if (_id < 0)
                _id = _count - 1;
        }

        public void Draw(SpriteBatch spritebatch)
        {
            for (int i = 0; i < _count; i++)
            {
                Texture2D blocTexture = BlocSet.GetSingleton()[_bloc[i]].Image;
                spritebatch.Draw(blocTexture,
                    new Rectangle((int)_position.X + i * blocTexture.Bounds.Width, (int)_position.Y, blocTexture.Bounds.Width, blocTexture.Bounds.Height),
                    Color.White);
            }
            spritebatch.Draw(_select,
                    new Rectangle((int)_position.X + _id * 16 - 2, (int)_position.Y - 2, _select.Bounds.Width, _select.Bounds.Height),
                    Color.White);
        }
        #endregion
    }
}
